package com.timmy.google_play.holder.detail;

import android.view.View;

import com.timmy.google_play.R;
import com.timmy.google_play.base.BaseHolder;
import com.timmy.google_play.base.MyApplication;
import com.timmy.google_play.bean.ItemBean;
import com.timmy.google_play.manager.DownLoadInfo;
import com.timmy.google_play.manager.DownLoadManager;
import com.timmy.google_play.utils.CommonUtils;
import com.timmy.google_play.utils.PrintDownLoadInfo;
import com.timmy.google_play.utils.UIUtils;
import com.timmy.progressbtn.ProgressBtn;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Timmy on 2016/12/14.
 * 描    述: 变成了观察者,可以收到观察的信息
 */
public class DetailBottomHolder extends BaseHolder<ItemBean> implements DownLoadManager.DownLoadInfoObserver {

    @BindView(R.id.app_detail_download_btn_download)
    ProgressBtn mAppDetailDownloadBtnDownload;
    private ItemBean mData;

    @Override
    protected View initHolderVIew() {
        return View.inflate(UIUtils.getContext(), R.layout.item_detail_bottom, null);
    }

    /**
     * 决定视图和数据的绑定
     */
    @Override
    protected void refreshHolderView(ItemBean data) {

        mData = data;

        /*================ 2.根据不同的状态,给用户不同的提示 =================*/
        DownLoadInfo downLoadInfo = DownLoadManager.getInstance().getDownLoadInfo(data);
        refreshProgressBtnUI(downLoadInfo);

    }

    /**
     * 刷新 button ui
     *
     * @param downLoadInfo
     */
    private void refreshProgressBtnUI(DownLoadInfo downLoadInfo) {
        int curState = downLoadInfo.curState;

        mAppDetailDownloadBtnDownload.setBackgroundResource(R.drawable.selector_app_detail_bottom_normal);
        switch (curState) {
            case DownLoadManager.STATE_UNDOWNLOAD:      //未下载

                //设置按钮文本
                mAppDetailDownloadBtnDownload.setText("下载");

                break;
            case DownLoadManager.STATE_DOWNLOADING:     //下载中
                mAppDetailDownloadBtnDownload.setProgressEnable(true);
                mAppDetailDownloadBtnDownload.setBackgroundResource(R.drawable.selector_app_detail_bottom_downloading);

                long max = downLoadInfo.max;
                long progress = downLoadInfo.progress;

                int progress_ = (int) (progress * 1.0f / max * 100 + 0.5f);
                mAppDetailDownloadBtnDownload.setText(progress_ + "%");
                mAppDetailDownloadBtnDownload.setMax(max);
                mAppDetailDownloadBtnDownload.setProgress(progress);

                break;
            case DownLoadManager.STATE_PAUSEDOWNLOAD:   //暂停下载
                mAppDetailDownloadBtnDownload.setText("继续下载");

                break;
            case DownLoadManager.STATE_WAITINGDOWNLOAD: //等待下载
                mAppDetailDownloadBtnDownload.setText("等待中...");

                break;
            case DownLoadManager.STATE_DOWNLOADFAILED:  //下载失败
                mAppDetailDownloadBtnDownload.setText("重试");

                break;
            case DownLoadManager.STATE_DOWNLOADED:      //下载完成
                mAppDetailDownloadBtnDownload.setText("安装");
                mAppDetailDownloadBtnDownload.setProgressEnable(false);
                break;
            case DownLoadManager.STATE_INSTALLED:       //安装成功
                mAppDetailDownloadBtnDownload.setText("打开");
                break;
        }
    }

    @OnClick(R.id.app_detail_download_btn_download)
    public void onClick() { 
        /*================ 3.根据不同的状态触发不同的操作 =================*/

        DownLoadInfo downLoadInfo = DownLoadManager.getInstance().getDownLoadInfo(mData);

        int curState = downLoadInfo.curState;

        switch (curState) {
            case DownLoadManager.STATE_UNDOWNLOAD:      //未下载
                //去下载
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManager.STATE_DOWNLOADING:     //下载中
                //点击暂停
                doPause(downLoadInfo);
                break;
            case DownLoadManager.STATE_PAUSEDOWNLOAD:   //暂停下载
                //点击继续下载
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManager.STATE_WAITINGDOWNLOAD: //等待下载
                doCancle(downLoadInfo);
                break;
            case DownLoadManager.STATE_DOWNLOADFAILED:  //下载失败
                doDownLoad(downLoadInfo);
                break;
            case DownLoadManager.STATE_DOWNLOADED:      //下载完成
                doInstall(downLoadInfo);
                break;
            case DownLoadManager.STATE_INSTALLED:       //安装成功
                doOpen(downLoadInfo);
                break;
        }

        /*
        //点击触发下载数据
        //添加和下载相关的参数
        DownLoadInfo downLoadInfo = new DownLoadInfo();
        downLoadInfo.downLoadUrl = mData.downloadUrl;
        String dir      = FileUtils.getDir("apk");           //sdcard/android/data
        String fileName = mData.packageName + ".apk";
        File   apkFile  = new File(dir, fileName);
        downLoadInfo.savePath = apkFile.getAbsolutePath();

        downLoadInfo.packageName = mData.packageName;

        DownLoadManager.getInstance().downLoad(downLoadInfo);
        */
    }

    /**
     * 开始下载,断点继续下载,重试下载
     */
    private void doDownLoad(DownLoadInfo downLoadInfo) {
        DownLoadManager.getInstance().downLoad(downLoadInfo);
    }

    /**
     * 暂停下载
     */
    private void doPause(DownLoadInfo downLoadInfo) {
        DownLoadManager.getInstance().doPause(downLoadInfo);
    }

    /**
     * 取消下载
     */
    private void doCancle(DownLoadInfo downLoadInfo) {
        DownLoadManager.getInstance().doCancle(downLoadInfo);
    }

    /**
     * 安装应用
     */
    private void doInstall(DownLoadInfo downLoadInfo) {
        File apkFile = new File(downLoadInfo.savePath);
        CommonUtils.installApp(UIUtils.getContext(), apkFile);
    }

    /**
     * 打开应用
     */
    private void doOpen(DownLoadInfo downLoadInfo) {
        CommonUtils.openApp(UIUtils.getContext(), downLoadInfo.packageName);
    }

    /*================ 接收到了最新 =================*/
    @Override
    public void onDownLoadInfoChange(final DownLoadInfo downLoadInfo) {

        //解决多个下载混乱问题
        if (!downLoadInfo.packageName.equals(mData.packageName)) {
            return;
        }

        //打印当前的状态
        PrintDownLoadInfo.printDownLoadInfo(downLoadInfo);

        //当前线程由发布消息的线程所决定
        //通知按钮更新
        MyApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                refreshProgressBtnUI(downLoadInfo);
            }
        });
    }
}
